﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Sites : AdminPage, ISitesView
    {
        private SitesPresenter _presenter;

        #region ISitesView Members

        public void SetSites(IList<Site> sites)
        {
            rpSites.DataSource = sites;
            rpSites.DataBind();
            if (sites.Count == 0)
                Response.Redirect("ConfigureFirstSite.aspx");
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Sites.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new SitesPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('sitePaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }

        protected void rpSites_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var site = (Site) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litDomain = (Literal) e.Item.FindControl("litDomain");
            var litOnline = (Literal) e.Item.FindControl("litOnline");
            var litMediaFolder = (Literal) e.Item.FindControl("litMediaFolder");
            var litLocale = (Literal) e.Item.FindControl("litLocale");
            var litMasterTemplate = (Literal) e.Item.FindControl("litMasterTemplate");
            var litDetailTemplate = (Literal) e.Item.FindControl("litDetailTemplate");

            litID.Text = "<span id=\"D" + site.ID + "\">";
            litDomain.Text = site.Domain;
            if (site.Online)
                litOnline.Text = GetText(this, "OnlineYes");
            else
                litOnline.Text = GetText(this, "OnlineNo");
            litMediaFolder.Text = site.RootMediaFolder;
            litLocale.Text = site.DefaultLocale.GetTranslation(Localization).Name;
            if (site.DefaultMasterTemplate != null)
                litMasterTemplate.Text = site.DefaultMasterTemplate.Name;
            if (site.DefaultDetailTemplate != null)
                litDetailTemplate.Text = site.DefaultDetailTemplate.Name;
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteSite(int.Parse(hidDeleteSiteID.Value.Substring(1)));
            upnlSites.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initSiteList",
                                                            "initSiteList(); location.reload(true);", true);
        }
    }
}